#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>

#include "DemoArray.h"

using namespace std;

void demoArrayLoop(void) {
    cout<<endl<<endl<<"demoArrayLoop"<<endl;
    
    vector<string> month {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"};
    
    for(vector<string>::iterator iter = month.begin(); iter != month.end(); iter++) {
        cout<<"demoArrayLoop, for-obj "<<*iter<<endl;
    }
    
    for(auto iter = month.begin(); iter != month.end(); iter++) {
        cout<<"demoArrayLoop, for-obj auto iterator "<<*iter<<endl;
    }
    
    // 反向迭代器
    for(auto iter = month.rbegin(); iter != month.rend(); iter++) {
        cout<<"demoArrayLoop, for-obj auto iterator reverse "<<*iter<<endl;
    }
    
    // 读写 auto 迭代器
    for(auto& iter : month) {
        iter = iter+":";
    }
    
    for(auto iter : month) {
        cout<<"demoArrayLoop, for auto "<<iter<<endl;
    }
}

// 如果是给 sort（升序排序）使用, 返回true, a 排在 b 前面
bool comp4Sort(const string & a,const string & b) {
    if (a.length() == b.length()) {
        return a.compare(b);
    }
    return a.length() > b.length();
}

void demoArraySort(void) {
    cout<<endl<<endl<<"demoArraySort"<<endl;
    
    vector<string> month {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"};
    
    // 拷贝构造
    vector<string> month4Sort(month);
    
    // 默认升序，在头文件 algorithm 中声明
    sort(month4Sort.begin(), month4Sort.end());
    for(auto iter : month4Sort) {
        cout<<"demoArraySort, default sort "<<iter<<endl;
    }
    
    // 默认降序
    reverse(month4Sort.begin(), month4Sort.end());
    for(auto iter : month4Sort) {
        cout<<"demoArraySort, reverse "<<iter<<endl;
    }
    
    // 升序，使用自定义比较函数
    sort(month4Sort.begin(), month4Sort.end(), comp4Sort);
    for(auto iter : month4Sort) {
        cout<<"demoArraySort, sort using comp4Sort "<<iter<<endl;
    }
    
}

void demoSetLoop(void) {
    cout<<endl<<endl<<"demoSetLoop"<<endl;
    
    srand((int)time(NULL));     //每次执行种子不同，生成不同的随机数
    set<int> numSet;
    for (int i=0; i<100; ++i) {
        numSet.insert(rand() % 100);
    }

    for(auto iter : numSet) {
        cout<<"numSet "<<iter<<endl;
    }
    
    cout<<"numSet count"<<numSet.size()<<endl;
}
